package com.dq.demo.model.http;

import android.app.Application;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Author : Z-JC
 * Date : 2020/1/15
 * Description :
 */
public class OkgoUtil {

    private static volatile OkgoUtil instance = null;

    public static OkgoUtil getInstance() {
        if (instance == null) {
            synchronized (OkgoUtil.class) {
                if (instance == null) {
                    instance = new OkgoUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 初始化okgo
     *
     * @param application
     */
    public void init(Application application) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .readTimeout(30 * 1000, TimeUnit.MILLISECONDS)
                .writeTimeout(30 * 1000, TimeUnit.MILLISECONDS)
                .connectTimeout(30 * 1000, TimeUnit.MILLISECONDS);
        OkGo.getInstance().setOkHttpClient(builder.build()).init(application);
    }

    /**
     * http Get请求
     *
     * @param url
     * @param stringCallback
     * @param map
     */
    public void get(String url, Map<String, String> map, StringCallback stringCallback) {
        OkGo.<String>get(url)
                .params(map)
                .retryCount(3)
                .execute(stringCallback);
    }

    /**
     * http Post请求
     *
     * @param url
     * @param stringCallback
     * @param map
     */
    public void post(String url, Map<String, String> map, StringCallback stringCallback) {
        OkGo.<String>post(url)
                .params(map)
                .retryCount(3)
                .execute(stringCallback);
    }

    /**
     * 取消全部请求
     */
    public void cancelAll() {
        OkGo.getInstance().cancelAll();
    }

}